/*
 * Copyright 2007-2008 Andrew O'Malley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package ws.quokka.plugin.maven;

import ws.quokka.core.bootstrap_util.Assert;
import ws.quokka.core.util.Strings;


/**
 *
 */
public class MavenId {
    //~ Instance fields ------------------------------------------------------------------------------------------------

    private String groupId;
    private String artifactId;
    private String type;
    private String version;
    private String classifier;

    //~ Constructors ---------------------------------------------------------------------------------------------------

    public MavenId() {
    }

    public MavenId(String groupId, String artifactId, String type, String classifier, String version) {
        this.groupId = groupId;
        this.artifactId = artifactId;
        this.type = type;
        this.classifier = classifier;
        this.version = version;
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getArtifactId() {
        return artifactId;
    }

    public void setArtifactId(String artifactId) {
        this.artifactId = artifactId;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getClassifier() {
        return classifier;
    }

    public void setClassifier(String classifier) {
        this.classifier = classifier;
    }

    public MavenId copy() {
        return new MavenId(groupId, artifactId, type, classifier, version);
    }

    public String toString() {
        return ns(groupId) + ":" + ns(artifactId) + ":" + ns(type) + ":" + ns(classifier) + ":" + ns(version);
    }

    private String ns(String string) {
        return (string == null) ? "" : string;
    }

    public static MavenId parse(String string) {
        String[] tokens = Strings.trim(Strings.splitPreserveAllTokens(string, ":"));
        Assert.isTrue(tokens.length <= 5, "Format for maven id is groupId:artifactId:type:classifier:version");

        int len = tokens.length;
        MavenId id = new MavenId();
        id.setGroupId((len >= 1) ? toNull(tokens[0]) : null);
        id.setArtifactId((len >= 2) ? toNull(tokens[1]) : null);
        id.setType((len >= 3) ? toNull(tokens[2]) : null);
        id.setClassifier((len >= 4) ? toNull(tokens[3]) : null);
        id.setVersion((len >= 5) ? toNull(tokens[4]) : null);

        return id;
    }

    private static String toNull(String string) {
        return string.equals("") ? null : string;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if ((o == null) || (getClass() != o.getClass())) {
            return false;
        }

        MavenId mavenId = (MavenId)o;

        if ((artifactId != null) ? (!artifactId.equals(mavenId.artifactId)) : (mavenId.artifactId != null)) {
            return false;
        }

        if ((classifier != null) ? (!classifier.equals(mavenId.classifier)) : (mavenId.classifier != null)) {
            return false;
        }

        if ((groupId != null) ? (!groupId.equals(mavenId.groupId)) : (mavenId.groupId != null)) {
            return false;
        }

        if ((type != null) ? (!type.equals(mavenId.type)) : (mavenId.type != null)) {
            return false;
        }

        if ((version != null) ? (!version.equals(mavenId.version)) : (mavenId.version != null)) {
            return false;
        }

        return true;
    }

    public int hashCode() {
        int result;
        result = ((groupId != null) ? groupId.hashCode() : 0);
        result = (31 * result) + ((artifactId != null) ? artifactId.hashCode() : 0);
        result = (31 * result) + ((type != null) ? type.hashCode() : 0);
        result = (31 * result) + ((version != null) ? version.hashCode() : 0);
        result = (31 * result) + ((classifier != null) ? classifier.hashCode() : 0);

        return result;
    }
}
